package com.zoweunion.mechanic.controller;

import java.util.Map;

import com.zoweunion.mechanic.entity.PromotionCouponEntity;
import com.zoweunion.mechanic.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zoweunion.mechanic.controller.BaseController;
import com.zoweunion.mechanic.util.constants.Constants;
import com.zoweunion.mechanic.util.MyException;
import com.zoweunion.mechanic.service.UserCouponService;
import com.zoweunion.mechanic.entity.UserCouponEntity;


import com.zoweunion.mechanic.model.ResponseBean;
import com.github.pagehelper.PageInfo;

import javax.servlet.http.HttpServletRequest;


/**
 * 优惠券用户关联表
 *
 * @author xp
 * @email 17688825585@163.com
 * @date 2020-08-11 13:34:07
 */
@RestController
@RequestMapping("/userCoupon")
public class UserCouponController extends BaseController {
    @Autowired
    private UserCouponService userCouponService;

    /**
     * 列表
     */
    @GetMapping("/userCouponList")
    public ResponseBean userCouponList(@RequestParam Map<String, Object> params, HttpServletRequest request) throws MyException {
        params.put("s_id", getCurrentUser(request).getS_id());
        params.put("user_id", getCurrentUser(request).getId());
        PageInfo info = userCouponService.queryPage(params);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", info);
    }

    /**
     * 新增
     */
    @PostMapping("/userCouponInsertUpdate")
    public ResponseBean userCouponInsertUpdate(@RequestBody UserCouponEntity params,
                                               HttpServletRequest request) {

        userCouponService.userCouponInsertUpdate(params);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", "");
    }

    /**
     * 首页弹框
     */
    @GetMapping("/userCouponHead")
    public ResponseBean userCouponHead(@RequestParam Map<String, Object> params, HttpServletRequest request) throws MyException {
        params.put("user_id", getCurrentUser(request).getId());
        PromotionCouponEntity entity = userCouponService.userCouponHead(params);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", entity);
    }


    /**
     * 优惠券赠送
     */
    @PostMapping("/giveCoupon")
    public ResponseBean giveCoupon(@RequestBody UserCouponEntity params,
                                   HttpServletRequest request) throws MyException {
        if (params.getGiveFlag()==1){
            params.setGiveUserId(getCurrentUser(request).getId());
        }
        return userCouponService.giveCoupon(params);
    }




}
